﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading.Tasks;

namespace Remember.WPF.Pages
{
    /// <summary>
    /// Interaction logic for TTS.xaml
    /// </summary>
    public partial class TTS : Window
    {
        System.Speech.Synthesis.SpeechSynthesizer speaker;

        public TTS()
        {
            InitializeComponent();

            speaker = new System.Speech.Synthesis.SpeechSynthesizer();

            if (speaker != null)
            {
                //s.Speak("Hello. My name is Microsoft Anna.");
                speaker.Speak("欢迎进入听说程序");
            }

            

        }

        Task taskTTS = null;
        System.Speech.Synthesis.SpeechSynthesizer speaker2;

        string Text = "";


        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            //s.Speak("Hello. My name is Microsoft Anna.");
            speaker.Speak("开始");

            // use a lambda expression and a named method
            Text = this.textBoxBody.Text;
            //Speck();

            taskTTS = new Task(() => Speck2());
            taskTTS.Start();
            // use a lambda expression and an anonymous method
        }

        void Speck2()
        {
            if (speaker2 == null)
            {
                speaker2 = new System.Speech.Synthesis.SpeechSynthesizer();
            }

            if (speaker2.State == System.Speech.Synthesis.SynthesizerState.Ready)
            {
                //s.Speak("Hello. My name is Microsoft Anna.");
                speaker2.Speak(Text);
            }
            else if (speaker2.State == System.Speech.Synthesis.SynthesizerState.Paused)
            {
                speaker2.Resume();
            }

        }

        void Speck()
        {
            if (speaker != null)
            {
                var speaker2 = new System.Speech.Synthesis.SpeechSynthesizer();
                //s.Speak("Hello. My name is Microsoft Anna.");
                speaker2.Speak( Text  );
            }

        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            if (speaker2 != null)
            {
                if (speaker2.State == System.Speech.Synthesis.SynthesizerState.Speaking)
                {
                    speaker2.Pause();
                }
                else if (speaker2.State == System.Speech.Synthesis.SynthesizerState.Paused)
                {
                    speaker2.Resume();
                }
            }
        }
    }
}
